package com.stgy.lease.web.admin.controller.attr;

import com.stgy.lease.common.redis.RedisPrefixEnum;
import com.stgy.lease.common.result.Result;
import com.stgy.lease.model.domain.AttrValue;
import com.stgy.lease.web.admin.service.AttrValueService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * ClassName:AttrValueController
 * Package:com.stgy.lease.web.admin.controller
 * Description:
 *
 * @Author:helei
 * @Create:2024/8/30 - 14:28
 */
@RestController
@RequestMapping("/admin/attr/value")
@Tag(name = "房间属性值管理模块")
public class AttrValueController {

    @Autowired
    private AttrValueService attrValueService;

    @Autowired
    private RedisTemplate redisTemplate;

    private String attrKeyVoListKey = RedisPrefixEnum.STGY_ADMIN_PREFIX + "attr:key:vo:list";

    @Operation(summary = "新增或者更新房间属性值")
    @PostMapping("/saveOrUpdate")
    public Result<AttrValue> saveOrUpdate(@RequestBody AttrValue attrValue) {
        boolean b = attrValueService.saveOrUpdateAttrValue(attrValue);
        if (b) {
            redisTemplate.delete(attrKeyVoListKey);
        }
        return Result.build(b);
    }

    @Operation(summary = "删除房间属性值")
    @DeleteMapping("/deleteById")
    public Result<String> deleteById(@RequestParam("id") List<Long> ids) {
        boolean b = attrValueService.removeByIds(ids);
        if (b) {
            redisTemplate.delete(attrKeyVoListKey);
        }
        return Result.build(b);
    }
}
